package com.hxrfid.project.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxrfid.common.constant.BasicConstants;
import com.hxrfid.common.core.domain.PageQuery;
import com.hxrfid.common.core.page.TableDataInfo;
import com.hxrfid.common.utils.StringUtils;
import com.hxrfid.project.domain.BsUserAddress;
import com.hxrfid.project.domain.bo.BsUserAddressBo;
import com.hxrfid.project.domain.vo.BsUserAddressVo;
import com.hxrfid.project.mapper.BsUserAddressMapper;
import com.hxrfid.project.service.IBsUserAddressService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 地址管理Service业务层处理
 *
 * @author hxrfid
 * @date 2025-06-12
 */
@RequiredArgsConstructor
@Service
public class BsUserAddressServiceImpl extends ServiceImpl<BsUserAddressMapper, BsUserAddress> implements IBsUserAddressService {

    private final BsUserAddressMapper baseMapper;

    /**
     * 查询地址管理
     * @param id 主键ID
     * @return 地址管理
     */
    @Override
    public BsUserAddressVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询地址管理列表
     * @param bo 地址管理对象
     * @param pageQuery 主键ID
     * @return 地址管理列表
     */
    @Override
    public TableDataInfo<BsUserAddressVo> queryPageList(BsUserAddressBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BsUserAddress> lqw = buildQueryWrapper(bo);
        Page<BsUserAddressVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询地址管理列表
     * @param bo 地址管理对象
     * @return 地址管理列表
     */
    @Override
    public List<BsUserAddressVo> queryList(BsUserAddressBo bo) {
        LambdaQueryWrapper<BsUserAddress> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    /**
    * 组装查询条件
    * @param bo 地址管理对象
    * @return 查询条件
    */
    private LambdaQueryWrapper<BsUserAddress> buildQueryWrapper(BsUserAddressBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BsUserAddress> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), BsUserAddress::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getGender()), BsUserAddress::getGender, bo.getGender());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), BsUserAddress::getPhone, bo.getPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), BsUserAddress::getAddress, bo.getAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getRoom()), BsUserAddress::getRoom, bo.getRoom());
        lqw.eq(StringUtils.isNotBlank(bo.getIsDefault()), BsUserAddress::getIsDefault, bo.getIsDefault());
        lqw.eq(bo.getUserId() != null, BsUserAddress::getUserId, bo.getUserId());
        return lqw;
    }

    /**
     * 新增地址管理
     * @param bo 地址管理对象
     * @return 是否成功
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public BsUserAddressBo insertByBo(BsUserAddressBo bo) {
        BsUserAddress add = BeanUtil.toBean(bo, BsUserAddress.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return bo;
    }

    /**
     * 修改地址管理
     * @param bo 地址管理对象
     * @return 是否成功
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Boolean updateByBo(BsUserAddressBo bo) {
        BsUserAddress update = BeanUtil.toBean(bo, BsUserAddress.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     * @param entity 地址管理对象
     */
    private void validEntityBeforeSave(BsUserAddress entity){
        // 判断当前地址是否是默认，如果是则取消其他地址的默认
        if (BasicConstants.STR_ONE.equals(entity.getIsDefault())) {
            baseMapper.update(null, Wrappers.<BsUserAddress>lambdaUpdate().eq(BsUserAddress::getUserId, entity.getUserId()).set(BsUserAddress::getIsDefault, BasicConstants.STR_ZERO));
        }
    }

    /**
     * 批量删除地址管理
     * @param ids 主键ID的list
     * @param isValid 是否业务校验
     * @return 是否成功
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
